<?php
/**
 * This file is form http://findcat.cn
 *
 * @link     https://findcat.cn
 * @email    1476982312@qq.com
 */
namespace App\Traits;

use Encore\Admin\Facades\Admin;
use Illuminate\Support\Facades\Schema;
use Encore\Admin\Auth\Database\Administrator;

trait AdminHelp
{
    /**
     * form 的admin_user_id设置.
     *
     * @param $form
     */
    protected function formAdminUser($form)
    {
        if (Schema::hasColumn($this->tableName, 'admin_user_id') && $this->currentId) {
            $form->displayE('admin_user_id', '操作人')
                ->with(function ($value) {
                    $adminUser = Administrator::find($value);

                    return $adminUser ? $adminUser->name : '';
                });
        }
    }

    /**
     * 自动设置adminUser
     * 只要model的表中有admin_user_id字段,就会自动设置管理端的操作人.
     *
     * @param $form
     */
    protected function autoAdminUserSaving($form)
    {
        $form->model()->admin_user_id = Admin::user()->id;
    }
}
